@include("/common/_html_start.html", {title: record.title!}){}
@include("/common/_header.html"){}
<style>
	em{
		color:red;
	}

</style>

<section class="container">
	<!--
	<div class="center">
		<h2></h2>
	</div>
	-->
	<div class="row">
		<div class="col-md-8">
			<input id="keyword" type="text" class="form-control" placeholder="+开心 +庆祝" value="开心 庆祝">
		</div>
		<div class="col-md-4">
			<button class="btn btn-default search">检索</button>
		</div>
	</div>

	<div class="row">
		<div class="col-md-12">
			<div class="comment_section">

			</div>
			<div class="content">

			</div>
			<div class="page_section"></div>
		</div>

		@/* @include("/blog/_blog_right.html"){} */
	</div>
</section>
@/*  @include("/common/_bottom.html"){} */
@/*  @include("/common/_footer.html"){} */
@include("/common/_html_common_js.html"){}
<script>
	var keyword = "";
	$(function() {
		$(".search").click(function() {
			keyword = $("#keyword").val();
			search(1);
		});

	});

	function search(pageNum) {
		$(".content").empty();
		$.ajax({
			url:"${ctxPath}/es/search",
			data:{pageNum:pageNum, text:keyword},
			success:function(data){
				console.log(data);
				var html = "";
				if(data.totalHits>0) {
					var pageCount = data.totalHits % 20 == 0 ? ( parseInt(data.totalHits / 20) ) : ( parseInt(data.totalHits / 20) + 1 );
					//$(".comment_section").html("当前第 "+pageNum+" 页，共 "+pageCount+" 页，"+data.totalHits+" 条");
					html += "";
					for(var i = 0; i < data.hits.length; i++){
						if(data.hits[i]){
							html += "<blockquote>";
							html += "<h3><ruby>"+data.hits[i].sourceAsMap.entry+"<rt>"+data.hits[i].sourceAsMap.pinyin+"</rt></ruby></h3>";
							html += "<p>"+data.hits[i].sourceAsMap.content+"</p>";
							html += "<small>DOCID:"+data.hits[i].sourceAsMap.id+"</small>";
							html += "</blockquote>";
						}
					}
					esPage(pageCount, pageNum)
				} else {
					$(".comment_section").html("");
					html = "<p class='text-danger'>一无所有</p>";
					$(".page_section").html("");
				}
				$(".content").html(html);

			}
		});
	}

	function esPage(totalPage, currentPage) {
		var pageHtml = "";
		if (totalPage <= 0 || currentPage > totalPage) return;
		var startPage = currentPage - 2;
		if(startPage < 1){
			startPage = 1;
		}
		var endPage = currentPage + 2;
		if(endPage > totalPage){
			endPage = totalPage;
		}
		pageHtml += "<ul class=\"pagination\">";
		if(currentPage <= 5){
			startPage = 1;
		}
		if(totalPage - currentPage<5){
			endPage = totalPage;
		}
		if(currentPage == 1){
			pageHtml += "<li class=\"disabled\"><a href=\"javascript:void(0)\">上页</a></li>";
		}else{
			pageHtml += "<li><a href=\"javascript:search("+(currentPage-1)+")\">上页</a></li>";
		}
		if(currentPage > 5){
			pageHtml += "<li><a href=\"javascript:search(1)\">1</a></li>";
			pageHtml += "<li><a href=\"javascript:search(2)\">2</a></li>";
			pageHtml += "<li><a href=\"javascript:void(0)\">...</a></li>";
		}
		var index = startPage;
		while(index <= endPage){
			if(currentPage == index){
				pageHtml += "<li class=\"active\"><a href=\"javascript:void(0)\">"+index+"</a></li>";
			}else{
				pageHtml += "<li><a href=\"javascript:search("+index+")\">"+index+"</a></li>";
			}
			index= index+1;
		}
		if((totalPage - currentPage) >= 5){
			pageHtml += "<li><a href=\"javascript:void(0)\">…</a></li>";
			pageHtml += "<li><a href=\"javascript:search("+(totalPage-1)+")\">"+(totalPage - 1)+"</a></li>";
			pageHtml += "<li><a href=\"javascript:search("+totalPage+")\">"+totalPage+"</a></li>";
		}
		if(currentPage == totalPage){
			pageHtml += "<li class=\"disabled\"><a href=\"javascript:void(0)\">下页</a></li>";
		}else{
			pageHtml += "<li><a href=\"javascript:search("+(currentPage+1)+")\" rel=\"next\">下页</a></li>";
		}
		pageHtml += "</ul>";
		$(".page_section").html(pageHtml);
	}
</script>



@include("/common/_html_end.html"){}

